/**
 * Project Name:kany-project-spring-boot
 * File Name:MyBatisConfig.java
 * Package Name:me.kany.project.spring.boot.config
 * Date:2016年5月10日下午3:59:04
 * Copyright (c) 2016, Jason.Wang All Rights Reserved.
 *
*/

package me.kany.project.spring.boot.config.mybatis;

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

import com.github.pagehelper.PageHelper;

/**
 * ClassName:MyBatisConfig <br/>
 * Function: MyBatis配置文件. <br/>
 * Date: 2016年5月10日 下午3:59:04 <br/>
 * 
 * @author Jason.Wang
 * @version
 * @since JDK 1.7
 * @see
 */
@Configuration
@EnableTransactionManagement
public class MyBatisConfig implements TransactionManagementConfigurer {

	@Autowired
	private DataSource dataSource;

	/**
	 * 
	 * sqlSessionFactoryBean:创建Spring-MyBatis.xml中的SQLSessionFactoryBean. <br/>
	 *
	 * @author Jason.Wang
	 * @return
	 * @since JDK 1.7
	 */
	@Bean(name = "sqlSessionFactory")
	public SqlSessionFactory sqlSessionFactoryBean() {
		try {
			SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
			// 配置数据源
			sqlSessionFactoryBean.setDataSource(dataSource);
			// 配置对象的包
			sqlSessionFactoryBean.setTypeAliasesPackage("me.kany.project.spring.boot.model");
			// 分页插件
			PageHelper pageHelper = new PageHelper();
			Properties properties = new Properties();
			// properties.setProperty("reasonable", "true");
			// properties.setProperty("supportMethodsArguments", "true");
			// properties.setProperty("returnPageInfo", "check");
			// properties.setProperty("params", "count=countSql");
			properties.setProperty("dialect", "mysql");
			properties.setProperty("offsetAsPageNum", "true");
			properties.setProperty("rowBoundsWithCount", "true");
			pageHelper.setProperties(properties);

			// 添加插件
			sqlSessionFactoryBean.setPlugins(new Interceptor[] { pageHelper });

			// 添加XML目录
			ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
			sqlSessionFactoryBean.setMapperLocations(resourcePatternResolver.getResources("classpath:me/kany/project/spring/boot/mapper/*.xml"));

			// 返回构建的SqlSessionFactory对象
			return sqlSessionFactoryBean.getObject();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	@Bean
	public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
		return new SqlSessionTemplate(sqlSessionFactory);
	}

	/**
	 * 
	 * 返回一个基于DataSource的事物管理器.
	 * 
	 * @see org.springframework.transaction.annotation.TransactionManagementConfigurer#annotationDrivenTransactionManager()
	 */
	@Override
	@Bean
	public PlatformTransactionManager annotationDrivenTransactionManager() {
		return new DataSourceTransactionManager(dataSource);
	}

}
